Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

underscore prototype imposing scalar context

by Aaronrp (Scribe)
on Mar 10, 2012 at 01:04 UTC ( [id://958820]=perlquestion: print w/replies, xml ) Need Help??

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

So, one of my favorite pieces of perl is Text::Trim, which uses contexts to do Just The Right Thing. Here is the trim() function, with some comments that I wrote:

sub trim { @_ = @_ ? @_ : $_ if defined wantarray; # When the caller is looking for a return value # (not void context), make a copy of the arguments, # so the original value is not changed. # If there are no arguments passed, # copy the value of $_ instead. for (@_ ? @_ : $_) { next unless defined; s/\A\s+//; s/\s+\z// } # If @_ contains any values, perform the # substitution on all defined values. If @_ contains # no values, perform the substitution on $_ instead. return @_ if wantarray || !defined wantarray; # return the changed values in array context # or (for a reason which escapes me) void context. if (my @def = grep defined, @_) { return "@def" } else { return } # In scalar context, return the results of # the substitution on all # defined values, if any. Otherwise just return. }

This code is pretty hard to read, mostly because of the punctuation variables. But it allows the caller to do

@trimmed = trim(@not_trimmed);


foreach (@untrimmed) { say trim; }



And the results make sense.

But, as with so many things, it doesn't work with lexical $_. I thought maybe I could get this to work with the new underscore _ prototype, but unfortunately that imposes scalar context on the argument. In other words,

sub trim (_;@) { # ... as above } my @data = (' a', ' b ' , 'c '); say trim (@data);

displays not "abc" but "3", since that's the value of @data in scalar context (the number of elements).

I can't think of any way around this that allows the flexibility of the Text::Trim calling conventions and also the use of lexical $_.

Any ideas?

Replies are listed 'Best First'.
Re: underscore prototype imposing scalar context
by BrowserUk (Patriarch) on Mar 10, 2012 at 01:31 UTC

    Does this help?

    sub x(@;_) { print @_ ? @_ : $_ };; $_ = 'fred'; x();; fred $_ = 'fred'; x( 1 .. 5 );; 1 2 3 4 5 $_ = 'fred'; x( 1 );; 1

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      No, that doesn't work. The unbackslashed @ eats up all the prototype characters behind it.


      #!/ActivePerl/bin/perl use 5.014; use warnings; sub sayit (@;_); sayit (1, 2, 3); my @list = (4, 5, 6); sayit (@list); local $_ = 7; sayit; my $_ = 8; sayit; sub sayit (@;_) { my @list = @_; if (@list) { say "@list" } else { say "no list" } }

      yields this:

      Prototype after '@' for main::sayit : @;_ at line 6. Prototype after '@' for main::sayit : @;_ at line 21. 1 2 3 4 5 6 no list no list

      The first two lines are due to the warnings pragma.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-07-12 13:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.