Skip to content

Instantly share code, notes, and snippets.

@dex4er
Created March 13, 2012 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dex4er/2031050 to your computer and use it in GitHub Desktop.
Save dex4er/2031050 to your computer and use it in GitHub Desktop.
Perlowo 2012-03-13
foreach my $n (1..3) {
say $n;
}
# a tu już $n nie działa
use IO::File;
do {
my $fh = IO::File->new('/etc/passwd');
# coś tam robimy na pliku
}
# pierwszy jest już zamknięty
{
my $fh = IO::File->new('/etc/groups');
# coś tam robimy na drugim pliku
}
if (my $value = calculate() > 0) {
say "$value is positive";
} elsif($value < 0) {
say "$value is negative";
} else {
say "zero";
}
# tu też już nie ma $value
sub f {
$_ = shift;
}
my $_ = 1;
f(2);
say $f; # będzie 1, bez "my" byłoby 2
$ perl -Mstrict -E '{ my $n = 1 }; say $n; say "zdechnie wcześniej"'
Global symbol "$n" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.
my $file = '/etc/passwd';
open my $fh, $file or die $!;
print <$fh>;
my $a, $b = 1, 2;
# lokalne $a jest niezdefiniowane
# globalne $b = 1
# wartość 2 jest użyte w pustym kontekście
my ($a, $b) = (1, 2);
sub pow2 {
my ($n) = @_;
return $n ** 2;
}
use IO::File;
my $fh = IO::File->new('/etc/passwd');
# coś tam robimy na pliku
my $fh = IO::File->new('/etc/groups');
# coś tam robimy na drugim pliku
# ale pierwszy jest wciąż niezamknięty!
#!/usr/bin/perl
use Modern::Perl;
say foreach qw(3 2 1 start!);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment