Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

anyway to abbreviate use strict; use warnings;

by metaperl (Curate)
on Jan 08, 2009 at 15:17 UTC ( [id://734917] : perlquestion . print w/replies, xml ) Need Help??

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

Thanks to the tireless efforts of merlyn, I am prudent about starting all my code with
use strict; use warnings;
But I would like to have some way of shortening this typing... any way to do so?

Replies are listed 'Best First'.
Re: anyway to abbreviate use strict; use warnings;
by Fletch (Bishop) on Jan 08, 2009 at 15:20 UTC

    Get a better editor that supports skeletons / templates / snippets, and/or write yourself a make-new-program program which spits one out (trivial using, say, TT).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: anyway to abbreviate use strict; use warnings;
by shmem (Chancellor) on Jan 08, 2009 at 16:44 UTC

    Why? Too much typing? Are the programs you write that short for these two statements to make up a significant amount of verbiage?

    But well...

    1. use a template
    2. if you use vim, stick these lines into your .vimrc:
      map <C-P> i#!/usr/bin/perl<CR>use strict;<CR>use warnings;<CR><CR> ab usw use strict;<CR>use warnings
      and insert the #!-line and those uses with <Ctrl>-P in command mode or typing usw; in insert mode
    3. write something similar for emacs
    4. let others write your program headers...
    5. what moritz sayeth
Re: anyway to abbreviate use strict; use warnings;
by sathiya.sw (Monk) on Jan 08, 2009 at 16:03 UTC
    If you are using vim editor then, use perl-support plugin...

    You can read about it at here.
    Get that plugin at here.

    Not only that expansion, you can reduce lot more typing in that..

    Sathiyamoorthy
Re: anyway to abbreviate use strict; use warnings;
by moritz (Cardinal) on Jan 08, 2009 at 15:27 UTC
Re: anyway to abbreviate use strict; use warnings;
by ides (Deacon) on Jan 08, 2009 at 16:18 UTC

    I wrote a small script that will start out a new script or module from a Template-Toolkit template. So it auto populates some of the boilerplate things beyond just use strict. Like filling out the script name in comments, POD, etc. Have been using it for years, it goes a long way to helping keep your code formatting more consistent.

    Frank Wiles <frank@revsys.com>
    www.revsys.com

Re: anyway to abbreviate use strict; use warnings;
by Your Mother (Archbishop) on Jan 08, 2009 at 16:54 UTC
    use Moose; # :)
Re: anyway to abbreviate use strict; use warnings;
by rhesa (Vicar) on Jan 08, 2009 at 18:19 UTC
Re: anyway to abbreviate use strict; use warnings;
by eric256 (Parson) on Jan 08, 2009 at 16:31 UTC

    Shorter...not that i know of. However I did setup vim so that it uses a template whenever i create .pl files and puts that at the top of it automaticaly. Works out pretty nice.


    ___________
    Eric Hodges
Re: anyway to abbreviate use strict; use warnings;
by lakshmananindia (Chaplain) on Jan 09, 2009 at 06:30 UTC

    If you are using vim editor, then use autocommand feature in it

    Put the below line in your vimrc.

    autocmd bufnewfile *.pl so /home/username/template
    The template file has to be as follows.
    :insert
    Insert what you want.... #!/usr/bin/perl
    use strict;
    use warnings;
    .
Re: anyway to abbreviate use strict; use warnings;
by ruzam (Curate) on Jan 09, 2009 at 01:17 UTC

    'Copy' followed by 'Paste'

    Anything else is just bath water vs baby...

Re: anyway to abbreviate use strict; use warnings;
by zentara (Archbishop) on Jan 08, 2009 at 21:17 UTC
    It seems that it is such a common module usage, that they could make some sort of shortcut for it, like
    #!/usr/bin/perl use ws;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: anyway to abbreviate use strict; use warnings;
by pat_mc (Pilgrim) on Jan 08, 2009 at 20:17 UTC
    Hi, metaperl -

    Based on what everybody else wrote in response to your question I have the feeling I may have misunderstood your question. I thought you are looking for a way to save on typing for the modest two lines
    use strict; use warnings;
    If that, indeed, is what you are after (oh, come on!), all I can suggest is
    #! /usr/bin/perl -w use strict;
    Since you need to type the shebang line anyway this -w will save you 10 characters!

    Hope this helps.

    Pat
      Please not that 1) -w isn't the same use warnings (it acts globally, not only in a lexical scope), and 2) this won't be interpreted for modules at all.
        Thanks for the knowledgeable additions to my post, Moritz. Just for the record, however, I see no reference to lexical scope or the use of modules in metaperl's original question, hence my response (well, to say the truth - I was unaware of the facts you mention so I probably still would have suggested -w as an answer, even if metaperl had mentioned those restrictions in the original question).

        Pat
Re: anyway to abbreviate use strict; use warnings;
by xdg (Monsignor) on Jan 09, 2009 at 16:26 UTC

    You can create a module to do that with ToolSet. See ToolSet::SWC as an example.

    Personally, I created a module I call "XDG" like this:

    package XDG; our $VERSION = '0.05'; use base 'ToolSet'; ToolSet->use_pragma( 'strict' ); ToolSet->use_pragma( 'warnings' ); ToolSet->export( 'Carp' => 'carp croak confess', 'Data::Dump::Streamer' => 'Dump', 'File::Spec' => undef, 'Path::Class' => 'file dir', 'Scalar::Util' => 'refaddr reftype blessed', ); if ( $] >= 5.010 ) { ToolSet->use_pragma( 'feature', ':5.10' ); } else { ToolSet->export( 'Perl6::Say' => 'say' ); } 1; # true

    That's handy for one-liners, too:

    $ perl -MXDG -e ...

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: anyway to abbreviate use strict; use warnings;
by Marshall (Canon) on Jan 11, 2009 at 06:13 UTC
    The easy portable way for "warnings" is
    #!/usr/bin/perl -w # on the first line of file (*NIX or Windows). # on a Unix type system, you need all but the -w anyway. # a Windows box will see the -w and use it. # I always recommend the usage of: # use strict;

    "Strict" has a compile time penalty. "Warnings" has a run-time penalty (but not that much). Unless you are darn sure your program is working very well AND you need the extra performance, I would never turn "Warnings Off". Strict is a compile time option and very cheap. Always use that.

      I've seen a number of benchmarks for warnings, and there never was a run-time penalty.

      Rate w=1 w=0 w=1 3003/ms -- -1% w=0 3032/ms 1% -- Rate w=1 w=0 w=1 3002/ms -- -0% w=0 3010/ms 0% --

      Benchmark code

      I'm curious as to why you said compile-time. Some strict checks are done at run-time. But again, there's no penalty for strict either.

      Rate sr=0 sr=1 sr=0 2751/ms -- -0% sr=1 2759/ms 0% -- Rate sr=1 sr=0 sr=1 2735/ms -- -1% sr=0 2754/ms 1% --

      Benchmark code