Easiest Way to Get Started with Perl

The easiest way to get started with Perl is to use PerlBanjo.com.

Please copy the following source code to PerlBanjo.com and click the run button.

print "Hello World!\n";

Result:

Hello World!

Regular Expression

I wrtie an example of regular expressions with the latest way to write Perl.

use v5.36;

my $message = 'I like apples. I buy apples.';

say "Input:$message";

$message =~ s/apple/orange/g;

say "Outpu:$message";

Result:

Input:I like apples. I buy apples.
Outpu:I like oranges. I buy oranges.

Parsing CSV

Parse CSV in a file. , are changed to spaces.

use v5.36;

while (my $line = <DATA>) {
  chomp $line;
  
  my ($id, $name, $price) = split(',', $line);
  
  my $new_line = join(' ', $id, $name, $price);
  
  say $new_line;
}

__DATA__
1,Perl,2000
2,Database,3000
3,Web,4000

Result:

1 Perl 2000
2 Database 3000
3 Web 4000

Parsing UTF-8 CSV

Parse CSV written by UTF-8.

use v5.36;
use utf8;
use Encode 'decode', 'encode';

while (my $line = <DATA>) {
  $line = decode('UTF-8', $line);
  
  chomp $line;
  
  my ($id, $name, $price) = split(',', $line);
  
  my $new_line = join(' ', $id, $name, $price);
  
  say encode('UTF-8', $new_line);
}

# Plain UTF-8 CSV data

no utf8;

__DATA__
1,Perl,2000
2,あいう,3000
3,えお,4000

Result:

1 Perl 2000
2 あいう 3000
3 えお 4000

Related Informatrion