Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

override keywords "my" and "our"

by jeremygwa (Initiate)
on Mar 18, 2009 at 00:00 UTC ( #751313=perlquestion: print w/ replies, xml ) Need Help??
jeremygwa has asked for the wisdom of the Perl Monks concerning the following question:

Hello Great High Priests of the PERLy gates

as the title suggest, how do I go about overriding keywords "my" and "our" without using any third party modules..just code?
I have many scripts, which do many declares, and I do not want to manually edit them.
Thank you in advance for code help.

Jeremy

Comment on override keywords "my" and "our"
Re: override keywords "my" and "our"
by JavaFan (Canon) on Mar 18, 2009 at 00:08 UTC
    You can't from pure Perl - my and our are keywords, not functions, so they cannot be overridden.

    You may be able to archieve what you want (but since you don't say what you want, it remains a guess) by using an appropriate B:: module and muck around in the opcode tree.


      basically, I would like to initialize all variables to a certain value.
      hopes this helps you understand. thanks in advance.
      -Jeremy

        so...

        my $foo = 3; my $bar = "keyword mania"; my @blivitz = "(some set of value);

        within code blocks, subs, or suchlike, when you need them ...and only when you need them.

        For example:

        my $bar = "keyword mania"; ... (do something); ... (and something more); bax($bar); # send $bar to sub bax for further proce +ssing ...(more code) { # bare block my @blivitz = ("7", '144', '1.16', "bat",); # @blivitz values availab +le only in this block ... do something with the elements of @blivitz; } # end block sub bax { # and here's the sub, bax my $foo = "3"; ...do something to the value in $bar (a global) with $foo (availab +le only in the sub); return (something); }

        Make sure you understand scoping and the issues created by making your variables global.

        Unless that value happens to be undef for scalars and empty for array and hashes, you're out of luck.

        You would be better to always use strictures (use strict; use warnings;) and fix each error or warning by understanding why it is being generated and fixing the root cause rather than applying band aids to patch over the issue. Maybe you could tell us what the real issue is?


        True laziness is hard work

        For lexical variables, you may be able to do something with Lexical::Persistence.

        #!perl use warnings; use strict; use Lexical::Persistence; # Create a new dynamic closure. my $dc = Lexical::Persistence->new(); # Prime a variable with a value. $dc->set_context( _ => { '$cat' => "persistent" } ); # Call a function within it. $dc->call(\&function); exit; sub function { # $cat is already initialized. print "persistent cat is '", my($cat), "'\n"; }
Re: override keywords "my" and "our"
by perrin (Chancellor) on Mar 18, 2009 at 00:34 UTC
    Bad idea. You're going to make your life hell trying to fix whatever problem you have in this way. Your best solution for initializing all variables is probably to write something with PPI that parses the code and then modifies the scripts. I bet you could get 80% of the way there with simple regexes.
Re: override keywords "my" and "our"
by duff (Vicar) on Mar 18, 2009 at 01:03 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-08-28 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (255 votes), past polls