/
5to6-perlfunc.pod6
2186 lines (1370 loc) · 58.1 KB
/
5to6-perlfunc.pod6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=begin pod :tag<convert>
=TITLE Perl 5 to Perl 6 guide - Functions
=SUBTITLE Builtin functions in Perl 5 to Perl 6.
=head1 DESCRIPTION
A (hopefully) comprehensive list of Perl 5 builtin functions with their
Perl 6 equivalents with notes on variations between them where
necessary.
=head1 NOTE
This document is an
attempt to guide you from the functions in Perl 5's perlfunc document to
their equivalents in Perl 6. For full documentation on the Perl 6
functions, follow the links in this document to their respective documentation.
One general comment: Perl 6 takes its objects a lot more seriously than
Perl 5. In Perl 6, everything is an object, although the language is
flexible enough to not force you to work in an object oriented manner if
you do not wish to do so. What this does mean, however, is that a lot of
things that are function calls of the form C<function(@args)> are now
also method calls of the form C<@args.function> (In rare cases, there is
I<only> a method call). This should be obvious in the following text,
but it probably behooves you to get into that frame of mind now.
Also, unless otherwise stated, the use of the term "function" here will mean a
function in the style of C<func(@args)>, while "method" will refer to a
function in the style of C<@args.func>.
=head1 Alphabetical Listing of Perl Functions
=head2 Filetests
=item -X FILEHANDLE
=item -X EXPR
=item -X DIRHANDLE
=item -X
Perl 6 gives you a couple of options when it comes to file tests. You can do a
smart match (C<~~>) or you can call a method.
In Perl 6, you don't need to actually open a filehandle in the
traditional way (although you can) to do a filetest. You can simply append
C<.IO> to the filename. For instance, here is how to check if a file is
readable using smart match:
'/path/to/file'.IO ~~ :r
You can, of course, use an already opened filehandle. Here, using the file
handle C<$fh>, is an example, using the method syntax for the file test:
=for code :preamble<my $fh;>
$fh.r
Most of the former filetests have colon equivalents for use with smart match:
=table
:e Exists
:d Directory
:f File
:l Symbolic link
:r Readable
:w Writable
:x Executable
:s Size
:z Zero size
All of these tests can be used as methods (without the colon).
Three tests, however, I<only> have method equivalents:
=for code :preamble<my $fh;>
$fh.modified; # -M $fh
$fh.accessed; # -A $fh
$fh.changed; # -C $fh
The remaining filetests in Perl 5 do not appear to be implemented
in Perl 6.
The documentation for this can be found at
L<File test operators|/type/IO::Path#File_test_operators>.
There is more information on reading and writing files at
L<io|/language/io>. Also, the section on C<open()>
below may be helpful.
The Perl 6 ecosystem has a module C<P5-X> that mimics the original Perl 5
behaviour as much as possible in Perl 6.
=head2 abs
=item abs VALUE
Works as a function (C<abs($x)>), but also as a method. One gotcha,
however - method calls bind more tightly than C<->, so, for example,
C<-15.abs> evaluates as C<-(15.abs)> giving you C<-15>. In this example, you
would have to do something like C<(-15).abs>.
C<abs> also operates on C<$_> in the absence of a value, but not as a
function, and as a method you need to call it as C<.abs> rather than simply
C<abs>.
=head2 accept
=item accept NEWSOCKET, GENERICSOCKET
C<accept> is a method you can call on a server, e. g.
C<$server.accept()>. Instead of returning a packed address, it returns a
socket, most likely an IO::Socket object of some sort.
=head2 alarm
=item alarm SECONDS
C<alarm()> is no more. But it is possible to have code execute after a
certain time has elapsed, or at a given time:
Promise.in(5).then: { say "five seconds have passed" }
Promise.at(now + 5).then: { say "five seconds have passed" }
In Perl 6, this does *not* involve any (dummy) signals.
=head2 atan2
=item atan2 Y, X
Available as a function as well as being able to be used as a method.
For instance, these are equivalent:
atan2(100);
100.atan2;
=head2 bind
=item bind SOCKET, NAME
[NEEDS FURTHER RESEARCH] No sign of a socket-related C<bind()> in Perl 6. At a
guess, whatever socket binding is needed happens when you create a new socket
object.
=head2 binmode
=item binmode FILEHANDLE
Instead of this, you would use C<:bin> as the file mode when opening the
socket. E. g. C<my $fh = open("path/to/file", :bin);>
=head2 bless
=item bless REF, CLASSNAME
With the changes in class creation in Perl 6, this may find less use
than in Perl 5, and is a method as well as a function. The Perl 6 docs
say "Creates a new object of the same type as the invocant, uses the
named arguments to initialize attributes, and returns the created
object." If you're porting a module from Perl 5 to Perl 6, it's quite
possible you'll want to use C<new> for creating objects rather than
C<bless>, although there may be some situations in which the latter may
still be useful.
=head2 break
=item break
Not in Perl 6. For breaking out of C<given> blocks, you should probably take a
look at C<proceed> and C<succeed>
L<here|/language/control#proceed>.
=head2 caller
=item caller EXPR
There are a couple different ways to get at caller information in Perl 6.
The basic functionality is provided through L<callframe> now. However, Perl 6
constructs call frames for regular blocks, not just for subroutines, so there
are more frames to look through. The following will retrieve the basic
information that C<caller> can return:
my $frame = callframe(0); # OR just callframe()
my ($subroutine, $package);
if $frame.code ~~ Routine {
$subroutine = $frame.code.name;
$package = $frame.code.package;
}
my $file = $frame.file;
my $line = $frame.line;
Many of the other details returned by C<caller> are specific to Perl 5 and have
no meaning in Perl 6.
You can also get some of the information for the current frame or routine frame
by using the dynamic variables L«C<&?ROUTINE>|/language/variables#Compile-time_variables»,
L«C<&?BLOCK>|/language/variables#Compile-time_variables», L«C<$?PACKAGE>|/language/variables#Compile-time_variables»,
L«C<$?FILE>|/language/variables#Compile-time_variables», and L«C<$?LINE>|/language/variables#Compile-time_variables».
For many purposes, L<Backtrace> may provide an easier way to browse through the call stack.
The Perl 6 ecosystem has a module C<P5caller> which exports a C<caller>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 chdir
=item chdir EXPR
Works as it does in Perl 5 but B<must> take an argument. The behaviour
of C<chdir()> (with regards to looking at HOME and LOGDIR) is not supported.
In Perl 6, L<chdir> only changes the C<$*CWD> dynamic variable. It does
B<not> actually change the default directory from the OS's point of view; the
special dynamic-variable routine L«C<&*chdir>|/routine/&*chdir» can be used
for that, if needed.
This is done this way, because there is no concept of a "default directory
per OS thread". And since Perl 6 does not fork, but only does threading,
it was felt that the "current directory" concept should be in the C<$*CWD>
dynamic variable, which can be lexically scoped, and thus can be thread-safe.
The Perl 6 ecosystem has a module C<P5chdir> which exports a C<chdir>
function that mimics the original Perl 5 behaviour as much as possible,
including looking at HOME and LOGDIR.
=head2 chmod
=item chmod LIST
Functions as under Perl 5, with the difference that octal numbers are
represented differently (C<0o755> rather than C<0755>). You may also use
it as a method, e. g. C<$fh.chmod(0o755)>.
=head2 chomp
=item chomp VARIABLE
The behavior of C<chomp> is different than in Perl 5. It leaves the
target unaffected and I<returns> a copy of the target with a final logical
newline removed, e.g. C<$x = "howdy\n";$y = chomp($x);> results in C<$x>
containing "howdy\n" and C<$y> containing "howdy". Also works as a
method, e.g. C<$y = $x.chomp>. As with many other methods, also works
with assignment to modify the target in place, e.g. C<$x.=chomp> results
in C<$x> containing "howdy".
Note that C<chomp()> (without arguments) is not supported in Perl 6.
The Perl 6 ecosystem has a module C<P5chomp> which exports a C<chomp>
function that mimics the original Perl 5 behaviour as much as possible.
.head2 chop
=item chop VARIABLE
As with C<chomp>, in Perl 6, this returns the chopped string, rather than
chopping in place. I. e. C<$x = "howdy";$y = chop($x);> results in C<$x>
being "howdy" and C<$y> being "howd". Also works as a method: C<$y = $x.chop>.
Note that C<chop()> (without arguments) is not supported in Perl 6.
The Perl 6 ecosystem has a module C<P5chomp> which exports a C<chop>
function that mimics the original Perl 5 behaviour as much as possible.
.head2 chown
=item chown LIST
C<chown> is not in Perl 6.
=head2 chr
=item chr NUMBER
Similar to the Perl 5 version, coerces the target to an integer, and uses that
as a Unicode code point to return the relevant character. Can be used as a
function and a method:
chr(65); # "A"
65.chr; # "A"
Note that C<chr()> (without arguments) is not supported in Perl 6.
The Perl 6 ecosystem has a module C<P5chr> which exports a C<chr>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 chroot
=item chroot FILENAME
Apparently this is not in Perl 6.
=head2 close
=item close FILEHANDLE
As in Perl 5, closes a filehandle. Returns a boolean value. Both C<close
$fh> and C<$fh.close> will work.
Note that C<close()> (without arguments) is not supported in Perl 6.
=head2 closedir
=item closedir DIRHANDLE
Not supported in Perl 6.
The Perl 6 ecosystem has a module C<P5opendir> which exports a C<closedir>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 connect
=item connect SOCKET, NAME
Use L<connect|/routine/connect> from L<IO::Socket::Async|/type/IO::Socket::Async> for
an asynchronous socket or create a L<IO::Socket::INET|/type/IO::Socket::INET> socket
for a synchronous one.
=head2 continue
=item continue BLOCK
=item continue
Instead of a C<continue> block, you should use a C<NEXT> block. The
closest analog to a bare C<continue;> in Perl 5 appears to be
C<proceed>/C<succeed>.
=head2 cos
=item cos EXPR
Works as in Perl 5, but can be also used as a method, i. e.
C<(1/60000).cos>.
=head2 crypt
=item crypt PLAINTEXT, SALT
This appears not to have made it into Perl 6.
=head2 dbm functions
=item dbmclose HASH
=item dbmopen HASH, DBNAME, MASK
These functions have largely been superseded in Perl 5, and are unlikely to
ever turn up in Perl 6 (although any assumptions about the Perl 6 database
implementation may be premature).
=head2 defined
=item defined EXPR
Probably does what you expect, but technically it returns C<False> on
the type object, and C<True> otherwise. This may make more sense when
you realize that C<$num.perl> is the type C<Any> if you haven't assigned
anything to it, and the assigned value if you have. Can, of course be
used as a method: C<$num.defined>. And any newly created class can have
its own C<.defined> method, thereby deciding how and when it should be
considered undefined.
Note that C<defined()> (without arguments) is not supported in Perl 6.
The Perl 6 ecosystem has a module C<P5defined> which exports a C<defined>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 delete
=item delete EXPR
Perl 6 replaces this with the new adverb syntax, specifically the
C<:delete> adverb. E. g. C<my $deleted_value = %hash{$key}:delete;> and
C<my $deleted_value = @array[$i]:delete;>.
=head2 die
=item die LIST
Works similarly to the Perl 5 version, but Perl 6's Exception mechanism
may give you more power and flexibility than is available in Perl 5.
See L<exceptions|/language/exceptions>. To omit the stacktrace
and location, like Perl 5's C<die "...\n">, use:
note "...";
exit 1;
=head2 do
=item do BLOCK
Similar to the Perl 5 version. Note that there must be a space between the
C<do> and the block.
=item do EXPR
Has been replaced in Perl 6 by C<EVALFILE>.
=head2 dump
=item dump LABEL
According to S29, C<dump> has been... dumped.
=head2 each
=item each HASH
There is no exact equivalent, but you can use C<%hash.kv> which
returns a list of keys and values. For example:
C<< for %hash.kv -> $k, $v { say "$k: $v" } >>
Incidentally, what we have there with the C<< -> >> is called a pointy
block and, though there are a number of examples in the documentation,
there doesn't seem to be a really clear explanation of how they work.
L<https://design.perl6.org/S04.html#The_for_statement> may be of some
help here, as well as the design document at
L<https://design.perl6.org/S06.html#%22Pointy_blocks%22>. There is also
some information at L<https://en.wikibooks.org/wiki/Perl_6_Programming/Blocks_and_Closures#Pointy_Blocks>
The Perl 6 ecosystem has a module C<P5each> which exports an C<each>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 eof
=item eof FILEHANDLE
In Perl 6, this is not usable as a function, but only as a method. I. e.
C<$filehandle.eof>. Returns C<True> if at end of file.
=head2 eval
=item eval EXPR
=item eval EXPR
Replaced by L<EVAL|/routine/EVAL>. Note that C<EVAL> does not do any
L<exception handling|/language/exceptions>!
=head2 evalbytes
=item evalbytes EXPR
No equivalent.
=head2 exec
=item exec LIST
Nothing in Perl 6 exactly replicates the Perl 5 C<exec>. C<shell> and
C<run> are similar to Perl 5's C<system>, but C<exec>'s behavior of not
returning after executing a system command would have to be emulated by
something like C<shell($command);exit();> or possibly C<exit
shell($command);>.
Neither of these workarounds have the behavior (on Unix-like systems)
of I<replacing> your Perl program's process with the new program;
notably, they will not work for the practice in some long-running
daemons of periodically redoing exec on themselves to reset their state
or force operating-system cleanup. Nor will they serve C<exec>'s
function of returning stale resources to the operating system.
If you want C<exec> for these behaviors, you can use an C<exec*>
function via the C<NativeCall> interface. Consult your operating
system manual pages for C<exec> (or other similarly-named calls such
as C<execl>, C<execv>, C<execvp>, or C<execvpe>). (Beware: these
calls are not generally portable between Unix-like operating system
families.)
=head2 exists
=item exists EXPR
In Perl 6, this is not a function, but an adverb:
=for code :preamble<no strict;>
%hash{$key}:exists;
@array[$i]:exists;
=head2 exit
=item exit EXPR
Appears to do the same thing as in Perl 5.
=head2 exp
=item exp EXPR
Same as in Perl 5, but can also be used as a method: C<5.exp>;
=head2 fc
=item fc EXPR
Looks like it does the same thing as in Perl 5 except that calling it without
arguments is not supported in Perl 6.
The Perl 6 ecosystem has a module C<P5fc> which exports a C<fc>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 fcntl
=item fcntl FILEHANDLE, FUNCTION, SCALAR
Appears not to be in Perl 6.
=head2 __FILE__
=item __FILE__
Replaced by C<$?FILE> which is slightly different from C<__FILE__> in that
it is always an absolute path, rather than a relative one in the Perl 5 case.
The Perl 6 ecosystem has a module C<P5__FILE__> which exports a C<__FILE__>
term that mimics the original Perl 5 behaviour as much as possible.
=head2 fileno
=item fileno FILEHANDLE
The C<native-descriptor> method on C<IO::Handle> returns the equivalent of
C<fileno>.
The Perl 6 ecosystem has a module C<P5fileno> which exports a C<fileno>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 flock
=item flock FILEHANDLE, OPERATION
Currently unimplemented.
=head2 fork
=item fork
There is no built-in `fork` function. While it's possible to call it using
NativeCall, it's highly unlikely that the resulting process will be usable.
Perl 6 provides extensive support for, and internally uses, threads. However,
`fork` only clones the thread that called `fork`, resulting in a process that
will be missing its other threads, which will have been in unknown states and
probably holding locks. Even if a Perl 6 program doesn't knowingly start any
threads, the compiler may create some of its own in the process of precompilation,
and the VMs that Perl 6 runs on also create their own internal worker threads for
doing things like optimization and GC in the background. Thus, the presence of
threads is pretty much assured, and there's no reasonable way to make `fork`
reliably work in this case.
=head2 formats
=item format
=item formline PICTURE, LIST
Perl 6 does not have built-in formats.
=head2 getc
=item getc FILEHANDLE
Reads a single character from the input stream as in Perl 5. May now
also be used as a method: C<$filehandle.getc>
=head2 getpeername
=item getpeername SOCKET
S29 lists it, but the implementation does not seem clear or, for that matter,
implemented.
=head2 getpgrp
=item getpgrp PID
Does not appear to be implemented.
=head2 getpriority
=item getpriority WHICH, WHO
Does not appear to be implemented.
=head2 get and set functions
=item endpwent
=item getlogin
=item getpwent
=item getpwnam NAME
=item getpwuid UID
=item setpwent
The Perl 6 ecosystem has a module C<P5getpwnam> which exports the C<endpwent>,
C<getlogin>, C<getpwent>, C<getpwnam>, C<getpwuid> and C<setpwent> functions
that mimic the original Perl 5 behaviour as much as possible.
=item endgrent
=item getgrent
=item getgrgid GID
=item getgrnam NAME
=item setgrent
The Perl 6 ecosystem has a module C<P5getgrnam> which exports the C<endgrent>,
C<getgrent>, C<getgrgid>, C<getgrnam> and C<setgrent> functions that mimic the
original Perl 5 behaviour as much as possible.
=item endnetent
=item getnetbyaddr ADDR, ADDRTYPE
=item getnetbyname NAME
=item getnetent
=item setnetent STAYOPEN
The Perl 6 ecosystem has a module C<P5getnetbyname> which exports the
C<endnetent>, C<getnetent>, C<getnetbyaddr>, C<getnetbyname> and C<setnetent>
functions that mimic the original Perl 5 behaviour as much as possible.
=item endservent
=item getservbyname NAME, PROTO
=item getservbyport PORT, PROTO
=item getservent
=item setservent STAYOPEN
The Perl 6 ecosystem has a module C<P5getservbyname> which exports the
C<endservent>, C<getservent>, C<getservbyname>, C<getservbyport> and
C<setservent> functions that mimic the original Perl 5 behaviour as much
as possible.
=item endprotoent
=item getprotobyname NAME
=item getprotobynumber NUMBER
=item getprotoent
=item setprotoent STAYOPEN
The Perl 6 ecosystem has a module C<P5getprotobyname> which exports the
C<endprotoent>, C<getprotoent>, C<getprotobyname>, C<getprotobynumber> and
C<setprotoent> functions that mimic the original Perl 5 behaviour as much
as possible.
=item gethostbyname NAME
=item gethostbyaddr ADDR, ADDRTYPE
=item gethostent
=item sethostent STAYOPEN
=item endhostent
[NEEDS FURTHER RESEARCH] Apparently this range of functions are to be handled
by roles like User, Group, etc.
=head2 getsock*
=item getsockname SOCKET
=item getsockopt SOCKET, LEVEL, OPTNAME
[NEEDS FURTHER RESEARCH] These are likely implemented by some kind of
IO::Socket object, but details are unclear.
=head2 glob
=item glob EXPR
Used in an example in S32, but apparently unimplemented.
=head2 gmtime
=item gmtime EXPR
Like the various parts of C<localtime>, C<gmtime>'s functionality
appears to in the C<DateTime> object. To get a UTC version of a
C<DateTime> object for the current time, for instance, use C<my $gmtime
= DateTime.now.utc>.
The Perl 6 ecosystem has a module C<P5localtime> which exports a C<gmtime>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 goto
=item goto LABEL
=item goto EXPR
=item goto &NAME
The syntax for C<goto LABEL> is already accepted, but the runtime part of
C<goto> is not yet implemented. So this will result in a runtime error:
FOO: goto FOO; # Label.goto() not yet implemented. Sorry.
=head2 grep
=item grep BLOCK LIST
=item grep EXPR, LIST
Still in Perl 6, with the caveat that the block form now requires a
comma after the block. I.e. C<@foo = grep { $_ = "bars" }, @baz>. Can
also be used as a method: C<@foo = @bar.grep(/^f/)>
=head2 hex
=item hex EXPR
In Perl 6 an expression B<must> be specified.
Replaced by the adverbial form C<:16>. E. g. C<:16("aF")> returns 175.
Alternately, the same result can be achieved by using the C<.base> method:
C<0xaF.base(10)>
It just so happens that C<.Str> defaults to base 10, so if you just C<say
0xaF>, that will also print 175, but that may not be immediately obvious, so
may not be the best way to go for this.
The Perl 6 ecosystem has a module C<P5hex> which exports a C<hex>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 import
=item import LIST
Was never a builtin function in Perl 5 in the first place. In Perl 6,
typically, one declares functions as exportable or not, and all the
exportable ones are exported. Nevertheless, selective importing is
possible, but beyond the scope of this document. For details, see
L<this section|/language/5to6-nutshell#Importing_specific_functions_from_a_module>.
=head2 index
=item index STR, SUBSTR, POSITION
Works as in Perl 5. Can also now be used as a method:
C<"howdy!".index("how"); # 0>. Main difference with Perl 5 is that C<Nil> is
returned instead of C<-1> when the substring is not found. This is very
useful in combination with the C<with> command:
with index("foo","o") -> $index {
say "Found it at $index";
}
else {
say "Not found"
}
The Perl 6 ecosystem has a module C<P5index> which exports an C<index>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 int
=item int EXPR
There is a C<truncate> function in Perl 6 (also usable as a method) that
does what Perl 5's C<int> does. You may want to use that as a direct
translation of Perl 5 code, but in Perl 6, you can just as easily call
the C<.Int> method on the number. C<3.9.Int; # 3> and C<3.9.truncate>
are equivalent.
Please note that C<int> B<does> have a meaning in Perl 6. It is type that
can be used to indicate a native integer:
my int $a = 42; # a native integer, similar to Perl 5's integer values
Because of this, it has proven impossible to create a Perl 6 module that
would export a sub that would mimic the Perl 5 behaviour.
=head2 ioctl
=item ioctl FILEHANDLE, FUNCTION, SCALAR
Currently unimplemented in Perl 6.
=head2 join
=item join EXPR, LIST
Works as in Perl 5, and also works as a method: C<@x.join(",")>
=head2 keys
=item keys HASH
Works as in Perl 5, and can also be used as a method: C<%hash.keys>
=head2 kill
=item kill SIGNAL, LIST
=item kill SIGNAL
No pre-defined core alternative exists. A non-portable method can be to use
L<NativeCall|/language/nativecall>:
use NativeCall;
sub kill(int32, int32) is native {*};
kill $*PID, 9; # OUTPUT: «Killed»
To kill processes that were started by creating a L<Proc::Async>, use
L«C<Proc::Async.kill> method|/type/Proc::Async#method_kill».
=head2 last
=item last LABEL
=item last EXPR
=item last
Same as in Perl 5.
=head2 lc
=item lc EXPR
Works as in Perl 5, and also as a method: C<"UGH".lc>. In Perl 6 an
expression B<must> be specified.
The Perl 6 ecosystem has a module C<P5lc> which exports an C<lc>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 lcfirst
=item lcfirst EXPR
Does not exist in Perl 6.
The Perl 6 ecosystem has a module C<P5lcfirst> which exports an C<lcfirst>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 length
=item length EXPR
Replaced by C<chars>, typically used as a method (C<$string.chars>), but
also works as a function.
The Perl 6 ecosystem has a module C<P5length> which exports an C<length>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 __LINE__
=item __LINE__
Replaced by C<$?LINE>.
The Perl 6 ecosystem has a module C<P5__FILE__> which exports a C<__LINE__>
term that mimics the original Perl 5 behaviour as much as possible.
=head2 link
=item link OLDFILE, NEWFILE
See L<link>
=head2 listen
=item listen SOCKET, QUEUESIZE
Not clearly documented, but it appears that C<listen> will be a method
you would call on some variety of IO::Socket object.
=head2 local
=item local EXPR
The Perl 6 equivalent is C<temp>. Unlike C<local>, however, the value of
the given variable is not immediately unset: it retains its original value
until assigned to.
=head2 localtime
=item localtime EXPR
Most of the functionality of C<localtime> is found in C<DateTime>. The
specific parts of C<localtime> can be found as follows:
=begin code
my $d = DateTime.now;
my $sec = $d.second; # Potentially includes fractional seconds
my $min = $d.minute;
my $hour = $d.hour;
my $mday = $d.day-of-month; # or $d.day; 1..31
my $mon = $d.month; # 1..12
my $year = $d.year;
my $wday = $d.day-of-week; # 1 => Monday, 2 => Tuesday, etc.
my $yday = $d.day-of-year; # 1..366
=end code
Please note that ranges are not 0-based in Perl 6, as shown in the
comments in the example.
There does not currently appear to be a way to get Perl 5's
C<$isdst>. Also, the result of C<scalar(localtime)> that Perl 5
provides is not available. C<$d.Str> will give something along the
lines of "2015-06-29T12:49:31-04:00".
The Perl 6 ecosystem has a module C<P5localtime> which exports a C<localtime>
function that mimics the original Perl 5 behaviour as much as possible.
=head2 lock
=item lock THING
There currently is no equivalent for this In Perl 6. There is a C<Lock>
class for creating a Lock object, that can be locked/unlocked as needed.
But such a lock does not refer to any external objects.
=head2 log
=item log EXPR
Available in Perl 6, but B<must> have an expression to work on. Also available
as a method. I. e. C<log(2)> is equivalent to C<2.log>.
=head2 lstat
=item lstat FILEHANDLE
=item lstat EXPR
=item lstat DIRHANDLE
=item lstat
Likely implemented somewhere in one of the C<IO> classes in Perl 6, but
it is not clear where at this time.
=head2 m//
=item m//
Regular expression syntax is somewhat different in Perl 6, but the match
operator still exists. If you're trying to rewrite some Perl 5 code, the
most important difference is that C<=~> is replaced by the smart match
operator, C<~~>. Similarly, C<!~> is replaced by C<!~~>. Options for
regex operators are adverbs and are complicated. For details, see
L<Adverbs|/language/regexes#Adverbs>
=head2 map
=item map BLOCK LIST
=item map EXPR, LIST
As a function, the only difference between Perl 5 and Perl 6 is that, if
you're using a block, the block must be followed by a comma. Can also be
used as a method: C<@new = @old.map: { $_ * 2 }>
=head2 mkdir
=item mkdir FILENAME, MASK
=item mkdir FILENAME
Works as in Perl 5. When giving a multi-level directory specification,
it will automatically create non-existing intermediate directories with
the same MASK (similar to what "make_path" does of the File::Path module
in Perl 5).
=item mkdir
The zero argument (implicit C<$_>) version is not permitted in Perl 6.
=head2 msg*
=item msgctl ID, CMD, ARG
=item msgget KEY, FLAGS
=item msgrcv ID, VAR, SIZE, TYPE, FLAGS
=item msgsnd ID, MSG, FLAGS
Not builtins in Perl 6. May appear in an external module at some point. Maybe.