Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Is it possible to overload backticks (qx) operator

by bdimych (Monk)
on Oct 21, 2008 at 14:32 UTC ( #718503=perlquestion: print w/replies, xml ) Need Help??
bdimych has asked for the wisdom of the Perl Monks concerning the following question:

overload::constant do the subject but do not give possibility to determine what operator qx or qq is being called
BEGIN { use overload; use Data::Dumper; overload::constant 'q' => sub { print Dumper \@_; }; } qq(echo aaa); qx(echo aaa);
prints two absolutely equal things:
$VAR1 = [ 'echo aaa', 'echo aaa', 'qq' ]; $VAR1 = [ 'echo aaa', 'echo aaa', 'qq' ];
?

Replies are listed 'Best First'.
Re: Is it possible to overload backticks (qx) operator
by cdarke (Prior) on Oct 21, 2008 at 16:27 UTC
    Overloading constant 'q' does not overload qq or qx, it overloads the string interpolation that these functions contain. Contrast with overloading ".
Re: Is it possible to overload backticks (qx) operator
by holli (Monsignor) on Oct 21, 2008 at 14:38 UTC
    It's not entirely equal. The qx example gives me command '1' not found. This indicates that the execution of the qx-result happens after your code is executed. So, maybe you want to override system() instead of qx?


    holli, /regexed monk/
      No, I do not receive "command '1'..." message. Probably it is because of difference of systems

      what I have:

      bdimych@bolshakovxp ~ $ perl -v This is perl, v5.10.0 built for cygwin-thread-multi-64int (with 6 registered patches, see perl -V for more detail) Copyright 1987-2007, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. bdimych@bolshakovxp ~ $ cat 1.pl BEGIN { use overload; use Data::Dumper; overload::constant 'q' => sub { print Dumper \@_; }; } qq(echo aaa); qx(echo aaa); bdimych@bolshakovxp ~ $ perl 1.pl $VAR1 = [ 'echo aaa', 'echo aaa', 'qq' ]; $VAR1 = [ 'echo aaa', 'echo aaa', 'qq' ]; bdimych@bolshakovxp ~ $ uname -a CYGWIN_NT-5.1 bolshakovxp 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygw +in
      In any case "not found" occurs only after my overloading sub is called, but how to determine operator right inside overloading sub?
Re: Is it possible to overload backticks (qx) operator
by Anonymous Monk on Oct 21, 2008 at 21:14 UTC
    What are you trying to do? What is your use case where it is necessary to interpret the contents of an interpolating string constant differently when it's going to be passed to the shell vs. when it's not, but for which it's not adequate simply to do
    sub my_qx { my $string = shift; # do stuff to $string, like you would in your "overload" return qx{$string}; } my $variable = my_qx q{stuff};
    ?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://718503]
Approved by holli
Front-paged by Old_Gray_Bear
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-01-18 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (206 votes). Check out past polls.

    Notices?