Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: What Are Your Live Journal Friends Interested In?

by rkg (Hermit)
on Jan 30, 2004 at 14:50 UTC ( #325242=note: print w/replies, xml ) Need Help??

in reply to What Are Your Live Journal Friends Interested In?

Neat -- a new trick I hadn't seen before -- creating and referencing a "my" variable all at once:
'user=s' => \my $TARGET_USER,

While I have no interest in LiveJournal, I do value folks who post small hiqh-quality scripts in their entirety -- it is nice to learn idioms and approaches from More Perl Savvy Folks than myself.

Thanks, ovid.

Replies are listed 'Best First'.
Re(2): What Is 'my' Interested In?
by Ovid (Cardinal) on Jan 30, 2004 at 15:56 UTC

    Thanks for the kind words.

    The my trick is an old one that relies on a feature of my that is not documented in perldoc -f my (though my is so ubiquitous that I suspect many people have not even read that perldoc). You see, my, like most Perl functions, has a return value. It returns variable it's declaring. That allows you to do stuff like this:

    chomp(my $data = <FH>); while ((my $foo = some_func()) eq 'bar') { ... }

    You can also use it to assign values to more than one variable:

    perl -MData::Dumper -e 'my @a = my ($x,$y,$z) = localtime;print Dumper \@a'

    In short, were it not for this feature, Perl would lose many nifty timesavers.


    New address of my CGI Course.

      Actualy, the last trick has nothing to do with the return of the my function -- an empty () would work just as well, and not effect what goes in @a at all.

      Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

        Either you are mistaken or I misunderstand you.
        $perl -MData::Dumper -we 'my @a = my ($x,$y,$z) = localtime;print Dump +er \@a' $VAR1 = [ 38, 27, 10 ]; $perl -MData::Dumper -we 'my @a = () = localtime;print Dumper \@a' $VAR1 = [];
        In both cases @a is assigned the result of the ...=localtime list assignment. And as perlop says, "a list assignment in list context produces the list of lvalues assigned to". In the my() case, the lvalues are the ones returned by my(). Though there seems to be a bug here:
        $perl -MData::Dumper -we 'my @a = my (undef,$y,$z) = localtime ;print Dumper \@a' $VAR1 = [ 49, 32, 10 ];
        I would have expected [undef,32,10] there.

      For Getopt::Long, I like to go one step further and have the defaults 'inlined'. Where Ovid wrote:

      GetOptions( ... 'verbose=i' => \my $VERBOSE, ... 'minimum=i' => \my $minimum, ); + $minimum ||= 1; $VERBOSE ||= 0;

      I like to write:

      GetOptions( ... 'verbose=i' => \(my $VERBOSE = 0), ... 'minimum=i' => \(my $minimum = 1), );

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://325242]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2023-05-30 18:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found