Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

passing $_ to my function

by arc_of_descent (Hermit)
on Sep 30, 2002 at 11:55 UTC ( [id://201685]=perlquestion: print w/replies, xml ) Need Help??

arc_of_descent has asked for the wisdom of the Perl Monks concerning the following question:


Hi Monks

How do i write a function which accepts $_
(of a loop), as its argument if none is passed?
Something like what the builtin print function
does.

I have the foll:
for (keys %online_users) { if (policy_is_expired($_)) { logoff_user($_); } }
I would like to eliminate the use of $_
in the loop.

Thanx

--
arc_of_descent

Replies are listed 'Best First'.
Re: passing $_ to my function
by broquaint (Abbot) on Sep 30, 2002 at 12:14 UTC
    Check if you've got any arguments otherwise default to $_ e.g
    sub def_arg { my $arg = @_ ? shift : $_; print "got: $arg\n"; } $_ = "bar"; def_arg("foo"); def_arg(); __output__ got: foo got: bar

    HTH

    _________
    broquaint

Re: passing $_ to my function
by moxliukas (Curate) on Sep 30, 2002 at 12:03 UTC

    Maybe something along the lines like:

    sub my_sub { my $argument = shift || $_; # do things with $argument }

    Be warned: I am not a Perl guru though

      That breaks if you pass an argument that evaluates as false. I'd do something like this:

      sub my_sub { my $arg = @_ ? shift : $_; # do stuff }
      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: passing $_ to my function
by arc_of_descent (Hermit) on Sep 30, 2002 at 12:47 UTC

    Thanx,

    I got what i wanted.
    Guess i have to read more about lexical scoping.
    I had no idea $_ would permeate
    through the function!

    --
    arc_of_descent

      Actually, this is dynamical scoping (local et al), not lexical (my et al).

      Makeshifts last the longest.

Re: passing $_ to my function
by robartes (Priest) on Sep 30, 2002 at 12:13 UTC
    You could try using the & form of a subroutine call:
    for (keys %online_users) { if (policy_is_expired($_)) { &logoff_user; } }
    This form passes @_ to the sub if no arguments are given.
      No, that won't work. You're passing @_, which is not the same as $_.

      Makeshifts last the longest.

Re: passing $_ to my function
by blm (Hermit) on Sep 30, 2002 at 14:06 UTC

    I usually use

    sub my_sub { my ($arg1, $arg2) = @_; ... }

    in my functions. So the equivalent of these would be:

    sub my_sub { my ($arg1, $arg2) = @_ ? @_ : $_; ... }
    Is this reasonable?
    --blm--
    If you don't like this post can you please /msg me
Re: passing $_ to my function
by nutshell (Beadle) on Sep 30, 2002 at 20:17 UTC
    Tell me, please, what's wrong with simply using:
    for my $i (keys %online_users) { if (policy_is_expired($i)) { logoff_user($i); } }
    Or just continuing to use $_?

    Implanting a work-around is just adding unneeded code.

      Implanting a work-around is just adding unneeded code.

      In addition, it is a waste of CPU time (cf Nicholas Clark's talk in Munich)

      Update: this talk is available at http://www.ccl4.org/~nick/P/Fast_Enough.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found