I have what is probably a fairly simple question that is baffling me. I
have some code that is basically a very simple search and replace routine.
I want to place it in a subroutine, do the specified function, and return
a value. The code itself works. However, when i try to place it in a sub,
it doesn't return anything, nor does it give errors. i've attached a
relevant code snippet so you can see what i'm talking about. BTW, this is
perl 5.6.0 on OpenBSD 2.9.
Thanks a lot guys (and gals),
Josh
-- BEGIN WORKING CODE --
$source = "blah";
%map = ('a' => 'foo',
'b' => 'bar'); #etc, etc, etc.
@chars = split //, $source;
$new = "";
foreach (@chars) {
$new .= (defined ($map{$_}) ? $map{$_} : $_);
}
# do more specialized replaces here...
$new =~ s/quxx/larry/g;
$new =~ s/(\w+)\./$1!/g;
# etc. etc. etc.
-------------------------------
I'd like to have:
------------------
%map = ('a' => 'foo',
'b' => 'bar'); #etc, etc, etc.
sub dostuff {
$source = shift;
@chars = split //, $source;
$new = "";
foreach (@chars) {
$new .= (defined ($map{$_}) ? $map{$_} : $_);
}
# do more specialized replaces here...
$new =~ s/quxx/larry/g;
$new =~ s/(\w+)\./$1!/g;
# etc. etc. etc.
return $new;
}
$newstring = dostuff "Hello, World.";
print $newstring;
---------------------------------
------------------------------------------------------------------
Josh Ghiloni - jo...@joshghiloni.net - http://www.joshghiloni.net/
Webmaster, WRUW-FM <http://www.wruw.org/>
Postmaster, The Geek Empire <http://www.thegeekempire.net/>
------------------------------------------------------------------
Hello, World!
just like it should.
The only real criticism is that you should make the variables used in
the sub lexical so that when you sub is run it does not alter variables
in the calling workspace. That means your should put "my" in front of
them the first time they are used in the sub. One could clean up the
code a lot and make it run much faster, but that wasn't your issue.
--
Bob Walton
>Baby's first post to comp.lang.perl.misc :)
Welcome!
There are some Posting Guidelings that may be helpful to you:
http://mail.augustmail.com/~tadmc/clpmisc.shtml
You should start all your Perl programs like this:
#!/usr/bin/perl -w
use strict;
That is, enable warnings and strictures.
>I have what is probably a fairly simple question that is baffling me.
It is baffling me too.
>The code itself works. However, when i try to place it in a sub,
>it doesn't return anything, nor does it give errors.
Have you put a print() statement or something inside of the
subroutine body to see if it is actually getting called?
Are you running this from the command line?
>I'd like to have:
Your subroutine works fine for me.
But your test string doesn't contain any of %map's keys :-)
After fixing the useless test into a more useful one, your
subroutine still works fine for me.
>%map = ('a' => 'foo',
> 'b' => 'bar'); #etc, etc, etc.
>
>sub dostuff {
> $source = shift;
> @chars = split //, $source;
You do not need either of those temporary variables.
> $new = "";
>
> foreach (@chars) {
foreach ( split //, shift ) {
> $new .= (defined ($map{$_}) ? $map{$_} : $_);
> }
>
> # do more specialized replaces here...
> $new =~ s/quxx/larry/g;
> $new =~ s/(\w+)\./$1!/g;
> # etc. etc. etc.
>
> return $new;
>}
>
>$newstring = dostuff "Hello, World.";
>print $newstring;
--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas
> I'd like to have:
> ------------------
> %map = ('a' => 'foo',
> 'b' => 'bar'); #etc, etc, etc.
>
> sub dostuff {
> $source = shift;
> @chars = split //, $source;
> $new = "";
>
> foreach (@chars) {
> $new .= (defined ($map{$_}) ? $map{$_} : $_);
> }
>
> # do more specialized replaces here...
> $new =~ s/quxx/larry/g;
> $new =~ s/(\w+)\./$1!/g;
> # etc. etc. etc.
>
> return $new;
> }
>
> $newstring = dostuff "Hello, World.";
> print $newstring;
Hmmm, works ok for me:
$ perl test
Hello, World!
You will want to start using strictures and warnings:
#!/usr/bin/perl -w
use strict;
as you start to write larger more complicated code.
Jon
--
Two are better than one, because they have a good return for their
work: If one falls down, his friend can help him up... Though one
may be overpowered, two can defend themselves. A cord of three
strands is not quickly broken. -- Ecclesiastes 4:9,12 (NIV)
:
:The only real criticism is that you should make the variables used in
:the sub lexical so that when you sub is run it does not alter variables
:in the calling workspace. That means your should put "my" in front of
:them the first time they are used in the sub.
Will do that.
------------------------------------------------------------------
Josh Ghiloni - jo...@joshghiloni.net - http://www.joshghiloni.net/
Webmaster, WRUW-FM <http://www.wruw.org/>
>However, when i try to place it in a sub,
>it doesn't return anything, nor does it give errors. i've attached a
>relevant code snippet so you can see what i'm talking about. BTW, this is
>perl 5.6.0 on OpenBSD 2.9.
[snip]
Just a WAG. Maybe printed output is being generated, but is
being overwritten on screen by the next command prompt. Try
tacking "\n" onto the print, or redirect output to a file.