Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Returning a tied scalar

by salva (Canon)
on Apr 16, 2005 at 11:34 UTC ( [id://448448]=note: print w/replies, xml ) Need Help??


in reply to Returning a tied scalar

if you can live without OO, using lvalue subrutines with (\$) prototypes makes things cleaner:
sub int_monitor (\$ ) :lvalue { my $var=shift; print "$var\n"; $$var }; int_monitor(my $foo) = 14; print "$foo\n"; (int_monitor my $bar) = 15; print "$bar\n";
or another way:
sub int_monitor (\$;$) { my $var=shift; print "$var\n"; $$var=shift if @_ }; int_monitor my $foo => 14; print "$foo\n";

Replies are listed 'Best First'.
Re^2: Returning a tied scalar
by nobull (Friar) on Apr 16, 2005 at 13:15 UTC

    If you can live without OO, using lvalue subrutines with (\$) prototypes makes things cleaner:

    sub int_monitor (\$ ) :lvalue { my $var=shift; print "$var\n"; $$var };

    There is no need for a prototype which also means you don't need to forgo the OO if you don't want to.

    sub int_monitor:lvalue { my $var=\shift; print "$var\n"; $$var };
      Nobull writes:
      There is no need for a prototype which also means you don't need to forgo the OO if you don't want to.

      sub int_monitor:lvalue { my $var=\shift; print "$var\n"; $$var };

      Very neat! I guess that relies on two facts: that @_ contains aliases to the args (which I knew but had forgotten) and that shift returns the first element of the array, rather than a copy of it.

      Thanks for your suggestions,

      Markus

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found