Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there some way to make variables like $a and $b in regard to strict?
    primarykey
    data
    text
    <p>In light of Michael Carman's comment, I have decided to rewrite the question. Note that 11 comments appear before this edit, and give credence to Michael's observation that I did not write the question in a way that made it clear what I was asking. <hr/> <em>Question:</em> What is the standard--or <em>cleanest</em> way--to fake the special status that <code>$a</code> and <code>$b</code> have in regard to strict by simply importing a module? </p> <p>First of all some setup. The following works: </p> <pre><code>#!/bin/perl use strict; print "\$a=$a\n"; print "\$b=$b\n"; </code></pre> <p>If I add one more line: </p> <pre><code>print "\$c=$c\n"; </code></pre> <p>I get an error at compile time, which means that none of my <em>dazzling</em> print code gets to run. </p> <p>If I comment out <code>use strict;</code> it runs fine. Outside of strictures, <code>$a</code> and <code>$b</code> are mainly special in that <code>sort</code> passes the two values to be compared with those names. </p> <pre><code>my @reverse_order = sort { $b &lt;=&gt; $a } @unsorted; </code></pre> <p>Thus the main <em>functional</em> difference about <code>$a</code> and <code>$b</code>--even though Perl "knows their names"--is that you'd better know this when you sort, or use some of the functions in <a href="http://search.cpan.org/module?List::Util" rel="nofollow noreferrer">List::Util</a>. </p> <p>It's only when you use strict, that <code>$a</code> and <code>$b</code> become special variables in a whole new way. They are the only variables that strict will pass over without complaining that they are not declared.</p> <p><em>:</em> Now, I like strict, but it strikes me that if TIMTOWTDI (There is more than one way to do it) is Rule #1 in Perl, this is not very TIMTOWDI. It says that <code>$a</code> and <code>$b</code> are special and that's it. If you want to use variables you don't have to declare <code>$a</code> and <code>$b</code> are your guys. If you want to have three variables by adding <code>$c</code>, suddenly there's a whole other way to do it.</p> <p>Nevermind that in manipulating hashes <code>$k</code> and <code>$v</code> might make more sense:</p> <pre><code>my %starts_upper_1_to_25 = skim { $k =~ m/^\p{IsUpper}/ &amp;&amp; ( 1 &lt;= $v &amp;&amp; $v &lt;= 25 ) } %my_hash ;` </code></pre> <p>Now, I use and I like strict. But I just want <code>$k</code> and <code>$v</code> to be visible to <code>skim</code> for the most compact syntax. And I'd like it to be visible simply by </p> <pre><code>use Hash::Helper qw&lt;skim&gt;; </code></pre> <p>I'm not asking this question to know how to black-magic it. My "answer" below, should let you know that I know enough Perl to be dangerous. I'm asking if there is a way to make strict accept other variables, or what is the <em>cleanest</em> solution. The answer could well be no. If that's the case, it simply does not seem very TIMTOWTDI. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload