Dual-license your content for inclusion in The Perl 5 Wiki using this HOWTO, or join us for a chat on irc.freenode.net#PerlNet.

PerlNet:Sandbox

From PerlNet

Jump to: navigation, search

Welcome to the sandbox page. You are welcome to experiment with wiki syntax and markup here, without worrying about making changes to an exisitng article. Please be kind to other users playing in the sandbox and avoid messing with their changes.

You are encouraged to sign your changes with your signature, written using four tildes (~~~~). This allows other users to know the markup is yours, and when you made it. You are also encouraged to put your markup in a section. This can be done with the following markup:

 == Section title ==

When you're finished experimenting, help keep the sandbox tidy by removing your changes. Pleae respect others and be polite when playing in the sandbox.



Contents

Example code for jbt on PerlNet

#!/usr/bin/perl -w
use strict;

my $opt = shift @ARGV;

if ( $opt eq '+' or $opt eq "sum" ) {
         my $sum = 0;

        foreach(@ARGV) {
                $sum += $_;
        }
        print "Total of @ARGV is: $sum \n";
}

Alternative answer

 #!/usr/bin/perl -w
 use strict;
 use List::Util qw(sum);
 
 my $opt = shift @ARGV;
 
 if ( $opt eq '+' or $opt eq "sum" ) {
        print "Total of @ARGV is: ", sum(@ARGV), "\n";
 }

Hash slice fun

Given this:

          my @keys = qw/foo bar baz/;
          my @vals = qw/1 2 3/;

We want this:

          %data = ( foo => 1, bar => 2, baz => 3);

Do this to do it all at once:

          my %data = map { ($keys[$_], $vals[$_]) } (0 .. $#keys);

or

          my %data;
          @data{@keys} = @vals;

Fetching from sv.wiktionary.org with WWW::Mechanize

#!/usr/bin/perl -w
use strict;
use warnings;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new(agent => "DoddeBot");

# Get an article...
$mech->get("http://sv.wiktionary.org/wiki/sk%C3%A4gg");

# Follow the edit link...
$mech->follow_link(url_regex => qr{edit});

# Print the raw wiki source of the article.
print $mech->value('wpTextbox1');

However this does give a warning regarding UTF-8 handling:

 Parsing of undecoded UTF-8 will give garbage when decoding entities at /usr/lib/perl5/HTML/PullParser.pm line 83.

which is discussed more at http://search.cpan.org/~gaas/HTML-Parser-3.54/Parser.pm#Unicode

--PJF (talk) 11:17, 22 June 2006 (EST)

path error? by jbt

   % tutorial_create.pl Controller Users
   
   Can't locate Catalyst/Helper.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site
   /lib .) at D:\Resources\Softwares n Utilities\CatInABox\bin\tutorial\script\tuto
   rial_create.pl line 6.
   BEGIN failed--compilation aborted at D:\Resources\Softwares n Utilities\CatInABo
   x\bin\tutorial\script\tutorial_create.pl line 6.

Answers

The error message suggests that Catalyst::Helper isn't where Perl thinks it should be.

If it's installed in:

    C:\Perl\Catalyst\Helper.pm

Then try:

    perl -I/Perl tutorial_create.pl Controller Users

and that should fix it.

If it's installed in:

   D:\Resources\Softwares n Utilities\Catalyst\Helper.pm

Then try:

   perl -I"D:/Resources/Softwares n Utilities/" tutorial_create.pl Controller Users

If this does fix it, you can make this a long term solution by changing your code as follows:

    #!/usr/bin/perl -w
    use strict;
    
    use lib '/Perl/';     # Add path to catalyst as determined above
    use Catalyst::Helper;


Experimental Script

#!/usr/bin/perl -w

use strict;

use win32::API;
my $newpasswd = "12345678";
open(FL,"list.txt") or die "can't open list.txt file $!\n";
my @list = <FL>;
foreach $wks (@list) {
         &ch_passwd($_,$newpasswd);
         }
print "\t Successly change password on listed computers \n";


 sub ch_passwd {
     my($pc_name,$passwd) = @_;
     my $function = Win32::API->new(
    'NetUserChangePassword','$pc_name','$passwd');
     my $return  = $function->Call($pc_name,$passwd);
      }
Personal tools