Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Note on usage of ours variables and a question

by tobyink (Canon)
on Nov 19, 2019 at 23:33 UTC ( #11108918=note: print w/replies, xml ) Need Help??


in reply to Note on usage of ours variables and a question

The following is a very, very stupid idea, but it does work.

${^MYDEBUG} = 1;

Variables written like that (names starting with a "^" sign and in braces) are automatically global and available everywhere in your program, without needing to be declared.

Have fun!

Replies are listed 'Best First'.
Re^2: Note on usage of ours variables and a question
by AnomalousMonk (Bishop) on Nov 20, 2019 at 02:13 UTC
    Variables written ... with a "^" sign and in braces ... are automatically global and available everywhere in your program, without needing to be declared.

    So is any fully-qualified package-global (even under strictures):

    c:\@Work\Perl\monks>perl -wMstrict -le "{ package Foo; $::bar = 'Baz'; sub p_bar { print $::bar; } } ;; Foo::p_bar; ;; { package Fiz; sub new_bar { $::bar = 'Boff'; } } Fiz::new_bar; Foo::p_bar; ;; $::bar = 'Quux'; print $::bar; " Baz Boff Quux


    Give a man a fish:  <%-{-{-{-<

Re^2: Note on usage of ours variables and a question
by likbez (Sexton) on Nov 20, 2019 at 02:06 UTC
    Hi Toby,

    Amazing finding !

    ${^MYDEBUG} = 1;
    This does solves that problem. So you are the first to provide a real solution. Kind of undocumented feature. Thank you very much.

    But the problem is that I often forget to put :: after $ and correcting this error ( as in "$debug -> $::debug ") takes my time and if you have many modules and change debugging statements in them often it became pretty annoying because for me this error is persistent.

    This is even more complex idiom and I unfortunately I am very bad with typing them. I still often type variables without sigil, especially if the same day I used C.

    But in any case I did learn something new and interesting about Perl today. Thanks again.

        Looks like one needs additional underscore:
        ${^_MYDEBUG} = 1;

        "No name that begins with ^_ will acquire a special meaning in any future version of Perl; such names may therefore be used safely in programs. $^_ itself, however, is reserved."

        "In particular, the special ${^_XYZ} variables are always taken to be in package main , regardless of any package declarations presently in scope."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2021-01-16 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?