[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Variables and References



On 7/30/98 at 7:55 AM, Jon Jacob and Joetta Moon <xavier@teleport.com> wrote:

>A file contains:
>
>--x--
>
>and I want to replace it with:
>
>$x
>
>so I write a line that states:
>
>$y =~ s/--x--/$x/g;
>
>I have to do that for a lot of diffent values, so I would like to standardize
>it.  The item between the dashes will always be the same as the name of the
>variable which is replacing it.  Can I somehow point to the variable with a
>reference of some sort.  I have not worked much with references and pointers
>so I am not totally clear how this might work.

Soft references will do this. Here's a contrived example:

#!perl
$str = "--x--";
$x = "foo";
$str =~ /--([a-z])--/;
$varname = $1; # now holds a soft ref to $x, i.e. "x"
$str =~ s/$str/$$varname/; # deref via "$$" syntax
print "$str\n"; # will print "foo"

See the perlref manpage and MJD's recent TPJ article on references:
<http://www.plover.com/~mjd/perl/FAQs/>

j


--
#!/usr/bin/perl
$dot="\x2e";$lt="\x3c";$gt="\x3e";$at="\x40";
print "J Mignault ${lt}mignault${at}nytimes${dot}com${gt}\n";
$yow = qq(.. I see TOILET SEATS...);



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch